Imports System.Security.Cryptography

Partial Class Admin_TestSQL
    Inherits System.Web.UI.Page

    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            If PasswordMatches() = False Then
                Response.Write("Incorrect Password")
                Exit Sub
            End If

            Me.SqlDataSource1.ConnectionString = EntityController.AppConnectString
            Me.SqlDataSource1.SelectCommand = Me.TextBox1.Text

            Dim args As New System.Web.UI.DataSourceSelectArguments()

            Me.GridView1.DataSource = Me.SqlDataSource1.Select(System.Web.UI.DataSourceSelectArguments.Empty)
            Me.GridView1.DataBind()
        Catch ex As Exception
            Response.Write(ex.Message)
            If Not ex.InnerException Is Nothing Then
                Response.Write(ex.InnerException.Message)
            End If
        End Try
        
    End Sub

    Private Function PasswordMatches() As Boolean
        Dim C_ENCRYPTED_PASSWORD = "CF9666633DADA27CAFF77B398E9FD1988202F1C0"

        Dim UTF As New UTF8Encoding()
        Dim Sha1 As New SHA1CryptoServiceProvider()
        Dim hashedDataBytes As Byte() = Sha1.ComputeHash(UTF.GetBytes(Me.PasswordTextBox.Text))
        Dim encryptedString As String = ByteArrayToString(hashedDataBytes)

        If encryptedString.Equals(C_ENCRYPTED_PASSWORD) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Function ByteArrayToString(ByVal hashedDataBytes As Byte()) As String
        Dim output As New StringBuilder("")
        For i As Integer = 0 To hashedDataBytes.Length - 1
            output.Append(hashedDataBytes(i).ToString("X2"))
        Next

        Return output.ToString()
    End Function




    '    public string sha1encrypt(string phrase) {
    'UTF8Encoding encoder = new UTF8Encoding();
    'SHA1CryptoServiceProvider sha1hasher = new SHA1CryptoServiceProvider();
    'byte[] hashedDataBytes = sha1hasher.ComputeHash(encoder.GetBytes(phrase));
    'return byteArrayToString(hashedDataBytes);
    '}
    'public string byteArrayToString(byte[] inputArray) {
    'StringBuilder output = new StringBuilder("");
    'for(int i = 0; i < inputArray.Length; i++) {
    'output.Append(inputArray[i].ToString("X2"));
    '}
    'return output.ToString();
    '}

End Class
